package com.ayoka.parenting.ui.coach.commentstudent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.ayoka.parenting.R;
import com.ayoka.parenting.adapter.MeditationQuestionAdapter;
import com.ayoka.parenting.bean.CommentBean;
import com.ayoka.parenting.bean.MeditationHistoryBean;
import com.ayoka.parenting.bean.MeditationQuestionBean;
import com.ayoka.parenting.bean.UserManager;
import com.ayoka.parenting.databinding.ActivityCommentStudentBinding;
import com.ayoka.parenting.interf.SimpleTextWatchListener;
import com.ayoka.parenting.mvp.MVPBaseRecyclerViewActivity;
import com.jincp.common.brvah.BaseQuickAdapter;
import com.jincp.common.brvah.BaseViewHolder;
import com.jincp.common.model.ToolBarOptions;
import com.jincp.image.load.ImageLoader;

import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: CommentStudentActivity.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class CommentStudentActivity extends MVPBaseRecyclerViewActivity<MeditationQuestionBean, ActivityCommentStudentBinding, CommentStudentContract.View, CommentStudentPresenter> implements CommentStudentContract.View {

    public static void start(Activity activity, String recordId) {
        Intent intent = new Intent(activity, CommentStudentActivity.class);
        intent.putExtra("RecordId", recordId);
        activity.startActivity(intent);
    }

    private String recordId;

    private CommentBean commentBean;


    @Override
    protected void configIntentValue() {
        Intent intent = getIntent();
        if (intent != null) {
            recordId = intent.getStringExtra("RecordId");
        }
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_comment_student;
    }

    @Override
    protected void initViews() {
        super.initViews();
        mDataBinding.edtContent.addTextChangedListener(new SimpleTextWatchListener() {
            @Override
            public void afterTextChanged(Editable s) {
                mDataBinding.btnSubmit.setEnabled(s.length() != 0);
            }
        });
        mDataBinding.tvComment.setOnClickListener(this);
        mDataBinding.btnCancel.setOnClickListener(this);
        mDataBinding.btnSubmit.setOnClickListener(this);
    }

    @Override
    protected SwipeRefreshLayout getRefreshLayout() {
        return mDataBinding.swipeRefresh;
    }

    @Override
    protected RecyclerView getRecyclerView() {
        return mDataBinding.recyclerView;
    }

    @NonNull
    @Override
    protected BaseQuickAdapter<MeditationQuestionBean, BaseViewHolder> getAdapter() {
        return new MeditationQuestionAdapter();
    }

    @Override
    protected RecyclerView.ItemDecoration getRecycleDivider() {
        return null;
    }

    @Override
    protected void getData(boolean isShowWaitDialog) {
        if (mPresenter != null) {
            mPresenter.getMeditationHistoryData(recordId);
        }
    }


    @Override
    protected void initData(Bundle savedInstanceState) {
        ToolBarOptions options = new ToolBarOptions(R.string.comment_sutdent);
        setCustomToolBar(mDataBinding.customToolbar.toolbar, options);

        getData(true);
    }


    @Override
    protected CommentStudentPresenter getPresenter() {
        return new CommentStudentPresenter();
    }

    @Override
    public void onClick(View view) {
        if (view == mDataBinding.tvComment) {
            switchInputCommentVisibility();
            mDataBinding.tvComment.setVisibility(View.GONE);
            mDataBinding.tvComment.setEnabled(false);
        } else if (view == mDataBinding.btnCancel) {
            switchInputCommentVisibility();
        } else if (view == mDataBinding.btnSubmit) {
            if (mDataBinding.edtContent.length() == 0) {
                showToast(getString(R.string.hint_input_comment));
                return;
            }
            String commentContent = mDataBinding.edtContent.getText().toString().trim();
            if (mPresenter != null) {
                mPresenter.submitComment(recordId, commentContent);
            }
        }
    }

    private void switchInputCommentVisibility() {
        if (mDataBinding.layoutInputComment.getVisibility() == View.VISIBLE) {
            mDataBinding.layoutInputComment.setVisibility(View.GONE);
        } else if (mDataBinding.layoutInputComment.getVisibility() == View.GONE) {
            mDataBinding.layoutInputComment.setVisibility(View.VISIBLE);
        }
        if (mDataBinding.layoutInputComment.getVisibility() == View.VISIBLE) {
            mDataBinding.layoutInputComment.setVisibility(View.GONE);
        }  else if (mDataBinding.layoutInputComment.getVisibility() == View.GONE) {
            mDataBinding.layoutComment.setVisibility(commentBean == null ? View.GONE : View.VISIBLE);
        }
    }

    @Override
    public void configMeditationHistoryData(MeditationHistoryBean data) {
        if (data == null) return;
        if (data.record != null && data.record.Meditation != null) {
            mDataBinding.tvMeditationTitle.setText(data.record.Meditation.GroupTitle + "：" +data.record.Meditation.Title);
            mDataBinding.tvMeditationDate.setText(data.record.LastUpdate);
            mDataBinding.tvComment.setVisibility(!TextUtils.equals(UserManager.getInstance().uid, data.record.Uid) ? View.VISIBLE : View.GONE);
        }
        if (data.questions != null) {
            setLoadMoreLayout(data.questions);
        }
        this.commentBean = data.comment;
        mDataBinding.layoutComment.setVisibility(commentBean == null ? View.GONE : View.VISIBLE);
        mDataBinding.tvComment.setText(commentBean == null ? R.string.comment : R.string.reComment);
        if (commentBean != null) {
            // TODO
//            mDataBinding.edtContent.setText(commentBean.Content);
//            mDataBinding.edtContent.setSelection(mDataBinding.edtContent.length());
//            ImageLoader.getInstance().displayImage(commentBean.);
        }
    }

    @Override
    public void onSubmitCommentCallback(boolean isSuccess) {
        if (isSuccess) {
            switchInputCommentVisibility();
            mDataBinding.tvComment.setVisibility(View.VISIBLE);
            mDataBinding.tvComment.setEnabled(true);
        }
    }
}
